package com.ego.seckill.controller;

import com.ego.seckill.access.AccessLimit;
import com.ego.seckill.pojo.SeckillGoods;
import com.ego.seckill.service.SeckillService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Ares
 * @date 2020-12-17
 */
@RestController
public class SeckillController {

    @Resource
    private SeckillService seckillService;

    @GetMapping("/list")
    public ResponseEntity<List<SeckillGoods>> list() {
        return ResponseEntity.ok(seckillService.findAll());
    }

    @AccessLimit(seconds=20,count=5)
    @GetMapping("/get_path/{seckillGoodsId}/{skuId}")
    public ResponseEntity<String> getPath(@PathVariable("seckillGoodsId")Long seckillGoodsId, @PathVariable("skuId")Long skuId){
        return ResponseEntity.ok(seckillService.createSeckillPath(seckillGoodsId, skuId));
    }

    @PostMapping("/{path}/seck")
    public ResponseEntity<String> seckill(@PathVariable("path") String path, @RequestBody SeckillGoods seckillGoods) {
        String orderId = seckillService.seckill(path, seckillGoods);
        return ResponseEntity.ok(orderId);
    }
}
